package com.shop.util;

import com.shop.entity.User;
import com.shop.service.UserService;
import com.shop.service.util.JwtUtil;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
@Service
public class UserTokenUtil {
    private static final String TOKEN_NAME = "user_token";
    private static final String COOKIE_NAME = "tokenCookie";

    /**
     * 创建用户 token
     * @param request HttpServletRequest 对象
     * @param response HttpServletResponse 对象
     * @param username 用户名
     * @param redisTemplate RedisTemplate redis操作对象
     * @return 返回创建的 token 字符串
     */
    public static String createUserToken(HttpServletRequest request, HttpServletResponse response, String username,RedisTemplate<String, Object> redisTemplate) {
        String token = JwtUtil.createTokenSingleInfo(20, COOKIE_NAME, username);
        CookieUtils.addCookie(response, COOKIE_NAME, token);
        redisTemplate.opsForValue().set(token,username,40, TimeUnit.MINUTES);
        return token;
    }
    /**
     * 获取用户的用户名
     * @param request HttpServletRequest 对象
     * @return 返回用户的用户名
     */
    public static String getUserName(HttpServletRequest request) {
        String tokenByCookie = CookieUtils.getCookieValueByName(request, COOKIE_NAME);

        return  JwtUtil.parseValueWithoutException(tokenByCookie, COOKIE_NAME);
    }
    /**
     * 获取用户信息
     * @param request HttpServletRequest 对象
     * @param userService UserService 对象
     * @return 返回获取到的用户对象，如果用户名为空则返回 null
     */
    public static User getUser(HttpServletRequest request, UserService userService) {
        String token = CookieUtils.getCookieValueByName(request, "tokenCookie");
        String username = JwtUtil.parseValueWithoutException(token, "username");
        return userService.getUser(username);
    }
    /**
     * 删除用户 token
     * @param request HttpServletRequest 对象
     * @param response HttpServletResponse 对象
     * @param redisTemplate RedisTemplate redis操作对象
     */
    public static void removeUserToken(HttpServletRequest request,HttpServletResponse response,RedisTemplate<String, Object> redisTemplate) {
        CookieUtils.addCookie(response,COOKIE_NAME,"",true);
        String tokenByCookie = CookieUtils.getCookieValueByName(request, COOKIE_NAME);
        if (!StringUtils.isEmpty(tokenByCookie)){
            redisTemplate.delete(tokenByCookie);
        }
    }
    /**
     * 检查是否需要延期 token
     * @param request HttpServletRequest 对象
     * @param response HttpServletResponse 对象
     * @param redisTemplate RedisTemplate redis操作对象
     */
    public static void checkIfNeedDelayToken(HttpServletRequest request,HttpServletResponse response,RedisTemplate<String, Object> redisTemplate){
        String tokenByCookie = CookieUtils.getCookieValueByName(request, COOKIE_NAME);
        if (Strings.isEmpty(tokenByCookie)){
            return;
        }
        try{
            JwtUtil.parseToken(tokenByCookie, COOKIE_NAME);
        }catch(ExpiredJwtException e) {
            String username = (String) redisTemplate.opsForValue().get(tokenByCookie);
            if (StringUtils.isEmpty(username)){
                return;
            }
            String token = JwtUtil.createTokenSingleInfo(20, COOKIE_NAME, username);
            CookieUtils.addCookie(response, COOKIE_NAME,token);
            redisTemplate.opsForValue().set(token, username,40,TimeUnit.MINUTES);
            redisTemplate.delete(tokenByCookie);
        }
    }

}
